package com.camelot.boot.server;

import okhttp3.*;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DeepSeekResumeExtractor {

    private static final String API_URL = "https://api.deepseek.com/chat/completions";
    private static final String API_KEY = "sk-dcb7da8d1cdc4a03bb27498e0a9c148a"; // 替换为你的 DeepSeek API 密钥

    /**
     * 从 PDF 文件中提取文本
     */
    public String extractTextFromPDF(File pdfFile) throws IOException {
        PDDocument document = null;
        try {
           // document = Loader.loadPDF(pdfFile);
            PDFTextStripper pdfStripper = new PDFTextStripper();
            return pdfStripper.getText(document);
        } finally {
            if (document != null) {
                document.close();
            }
        }
    }

    /**
     * 调用 DeepSeek API 提取关键信息
     */
    public String extractKeyInformation(String text) throws IOException {
        OkHttpClient client = new OkHttpClient();

        // 构建请求体
        String json = "{"
                + "\"model\": \"deepseek-chat\","
                + "\"messages\": ["
                + "  {\"role\": \"system\", \"content\": \"You are a helpful assistant that extracts key information from resumes.\"},"
                + "  {\"role\": \"user\", \"content\": \"Extract key information from the following resume text: " + text + "\"}"
                + "],"
                + "\"max_tokens\": 100000,"
                + "\"temperature\": 0.7"
                + "}";

        RequestBody body = RequestBody.create(json, MediaType.get("application/json; charset=utf-8"));
        Request request = new Request.Builder()
                .url(API_URL)
                .post(body)
                .addHeader("Authorization", "Bearer " + API_KEY)
                .addHeader("Content-Type", "application/json")
                .build();

        try (Response response = client.newCall(request).execute()) {
            System.out.println("222:"+response);
            return response.body().string();
        }
    }

    /**
     * 处理 DeepSeek API 响应，提取关键信息
     */
    public Map<String, List<String>> processApiResponse(String apiResponse) {
        Map<String, List<String>> extractedInfo = new HashMap<>();
        // 在这里解析 API 响应并提取关键信息
        // 示例：假设 API 返回的格式为 JSON，包含提取的信息
        // 实际解析逻辑需要根据 DeepSeek API 的具体响应格式进行调整
        // 可以使用 Gson 或其他 JSON 解析库来解析响应
        return extractedInfo;
    }

    /**
     * 完整的简历信息抽取流程
     */
    public Map<String, List<String>> extractResumeInfo(File pdfFile) throws IOException {
        String text = extractTextFromPDF(pdfFile);
        String apiResponse = extractKeyInformation(text);
        return processApiResponse(apiResponse);
    }

    public static void main(String[] args) {
        try {
            File resumePdf = new File("D:\\new1\\ruoyi-vue-pro\\boot-server\\src\\main\\resources\\resume.pdf"); // 替换为你的简历 PDF 文件路径
            DeepSeekResumeExtractor extractor = new DeepSeekResumeExtractor();
            Map<String, List<String>> info = extractor.extractResumeInfo(resumePdf);

            System.out.println("提取的简历信息:");
            info.forEach((key, value) -> {
                System.out.println(key + ": " + value);
            });

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}